package tq.com.tqteacher.net;

import android.app.Application;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

import tq.com.download.TQDownloadManager;
import tq.com.tqandroidim.TQIM;
import tq.com.tqcom.net.INetCallback;
import tq.com.tqcom.net.TQNetClient;
import tq.com.tqcom.net.TQNetResult;
import tq.com.tqcom.util.TQSystemDefine;
import tq.com.tqcom.view.TQClickButton;
import tq.com.tqteacher.model.TQAtachModel;
import tq.com.tqteacher.model.TQPublishType;
import tq.com.tqteacher.propertyEnum.TQModify;
import tq.com.upload.TQUploadFileManager;

public class TQNetInterface {
    static private String url = "http://banqun123.com:7000/";
    static public void setUrl(String url){
        TQNetInterface.url = url;
    }
    static public void init(final INetCallback pNetCallback){
//debug 5242, release 7000
        TQNetClient.getShareClient().setBaseUrl(TQNetInterface.url);
        TQNetClient.getShareClient().get("/svr", null, new INetCallback() {
            @Override
            public void result(TQNetResult pNetResult) {
                if(pNetResult.isSuccess()){

                    if(pNetResult.getData() == null) return;
                    JSONObject pBody = pNetResult.getData();

                    String server = pBody.optString("TeacherSvr");
                    String file = pBody.optString("FileSvr");
                    String im = pBody.optString("ImSvr");
                    setIm(im);

                    String filePath = String.format("%s/api/user/file/", file);

                    TQNetClient.getShareClient().setBaseUrl(server);
                    TQDownloadManager.setBaseUrl(filePath);
                    TQUploadFileManager.setUploadUrl(String.format("%s/api/user/file/new", file));
                }

                if(pNetCallback != null){
                    pNetCallback.result(pNetResult);
                }
            }
        });

//        TQNetClient.getShareClient().setBaseUrl("http://banqun123.com:5252/");
//        TQDownloadManager.setBaseUrl("http://banqun123.com:5252/api/user/file/");
//        TQUploadFileManager.setUploadUrl("http://banqun123.com:5252/api/user/file/new");
    }

    static public void setIm(String pIm){
        int index = pIm.indexOf(':');
        if(index == -1) return;

        String url = pIm.substring(0, index);
        String port = pIm.substring(index + 1);

        TQIM.getShareIm().getConfigure().setIp(url);
        TQIM.getShareIm().getConfigure().setPort(Integer.valueOf(port));
    }

    static public void setToken(String pToken){
        TQNetClient.getShareClient().setAuthToken(pToken);
        TQUploadFileManager.setToken(pToken);
        TQDownloadManager.addDefaultHeader("X-Auth-Token", pToken);

        TQIM.getShareIm().getConfigure().setToken(pToken);
        TQIM.getShareIm().start();
    }

    static public void login(String name, String pwd, INetCallback pNetCallback){
        HashMap<String, Object> lHashMap = new HashMap<>();
        lHashMap.put("name", name);
        lHashMap.put("pwd", pwd);

        TQNetClient.getShareClient().post("/api/teacher/session", lHashMap, pNetCallback);
    }


    static public void newPublish(HashMap<String, Object> param, INetCallback pNetCallback){
        TQNetClient.getShareClient().post("/api/material/new", param, pNetCallback);
    }

    static public void listUploadFile(int type, int page, INetCallback pNetCallback){
        String listType = "all";
        if(type == TQSystemDefine.FILE_TYPE_PDF){
            listType = "pdf";
        }else if(type == TQSystemDefine.FILE_TYPE_MP4){
            listType = "mp4";
        }

        String url = String.format("/api/user/file/list/%s/10/%d", listType, page);
        TQNetClient.getShareClient().get(url, null, pNetCallback);
    }

    static public void getHomeList(int cls, int page, INetCallback pNetCallback){
        String url = String.format("/api/material/clz/%d/20/%d", cls, page);
        TQNetClient.getShareClient().get(url, null, pNetCallback);
    }
    static public void getPublishLink(int cls, int subject, int page, INetCallback pCallback){
        String url = String.format("/api/material/clz/%d/subject/%d/chain_rc/20/%d", cls, subject ,page);
        TQNetClient.getShareClient().get(url, null, pCallback);
    }
    static public void getOptimizeList(int cls, int subject, int page, INetCallback pNetCallback){
        //String url = String.format("/api/material/clz/%d/20/%d", cls, page);
        String url = String.format("/api/material/clz/%d/subject/%d/20/%d", cls, subject, page);
        TQNetClient.getShareClient().get(url, null, pNetCallback);
    }

    static public void getMatrialDetail(int id, INetCallback pNetCallback){
        TQNetClient.getShareClient().get(String.format("/api/material/%d", id), null, pNetCallback);
    }
    static public void bindAttach(Integer matrialId, ArrayList<HashMap<String, String>> attach, INetCallback pNetCallback){
        HashMap<String, Object> lHashMap = new HashMap<>();
        lHashMap.put("material_id", matrialId);
        lHashMap.put("attachment", attach);

        TQNetClient.getShareClient().post("/api/material/attachment", lHashMap, pNetCallback);
    }

    static public void getClsInfo(String clsiD, INetCallback pNetCallback){
        TQNetClient.getShareClient().get(String.format("/api/clazz/%s/profile", clsiD), null, pNetCallback);
    }

    static public void getClsCardInfo(String clsId, INetCallback pNetCallback){
        TQNetClient.getShareClient().get(String.format("/api/clazz/%s/card", clsId), null, pNetCallback);
    }

    static public void getCardActivityInfo(int ClsId, int year, INetCallback pNetCallback){
        TQNetClient.getShareClient().get(String.format("/api/clazz/%d/card/activestat/%d", ClsId, year), null, pNetCallback);
    }
    static public void getCardSelaInfo(int clsId, int year, INetCallback pNetCallback){
        TQNetClient.getShareClient().get(String.format("/api/clazz/%d/card/salestat/%d", clsId, year), null, pNetCallback);
    }
    static public void modifyTeacherInfo(int id, TQModify.TeacherInfo pInfo, String value, INetCallback pCallback){
        HashMap<String, Object> lHashMap = new HashMap<>();
        lHashMap.put("name", pInfo.name());
        lHashMap.put("val", value);

        ArrayList<HashMap<String, Object>> fileds = new ArrayList<>();
        fileds.add(lHashMap);

        HashMap<String, Object> lAll = new HashMap<>();
        lAll.put("id", id);
        lAll.put("field", fileds);

        TQNetClient.getShareClient().post("/api/teacher/update", lAll, pCallback);
    }

    static public void modifyMaterialInfo(int id, HashMap<TQModify.MaterialBaseInfo, Object> info, INetCallback pNetCallback){
        if(info == null || info.size() == 0) return;

        ArrayList<HashMap<String, Object>> sets = new ArrayList<>();

        for (HashMap.Entry<TQModify.MaterialBaseInfo, Object> item:
             info.entrySet()) {
            HashMap<String, Object> modifyItem = new HashMap<>();

            modifyItem.put("name", item.getKey().name());
            modifyItem.put("val", item.getValue());

            sets.add(modifyItem);
        }

        HashMap<String, Object> param = new HashMap<>();
        param.put("id", Integer.valueOf(id));
        param.put("field", sets);
        TQNetClient.getShareClient().post("/api/material/update", param, pNetCallback);
    }

    static public void getNotice(int clsi, int page, int pageSize, INetCallback pCallback){
        String url = String.format("/api/notice/clz/%d/%d/%d", clsi, pageSize, page);

        TQNetClient.getShareClient().get(url, null, pCallback);
    }
    static public void newNotice(String content, INetCallback pNetCallback){
        HashMap<String, Object> lHashMap = new HashMap<>();
        lHashMap.put("content", content);

        TQNetClient.getShareClient().post("/api/notice/new", lHashMap, pNetCallback);
    }
    static public void getClazzUser(INetCallback pNetCallback){
        TQNetClient.getShareClient().get("/api/clazz/user", null, pNetCallback);
    }

    static public void delNotice(int id, INetCallback pNetCallback){
        HashMap<String, Object> lHashMap = new HashMap<>();
        lHashMap.put("id", id);

        TQNetClient.getShareClient().post("/api/notice/del", lHashMap, pNetCallback);
    }

    static public void attachFile2Material(int mId, ArrayList<HashMap<String, String>> att, INetCallback pNetCallback){
        HashMap<String, Object> param = new HashMap<>();
        param.put("material_id", mId);
        param.put("attachment", att);

        TQNetClient.getShareClient().post("/api/material/attachment", param, pNetCallback);
    }

    static public void modifyAttachInfo(int id, HashMap<TQModify.AttrachInfo, String> value, INetCallback pNetCallback){
        HashMap<String, Object> lHashMap = new HashMap<>();
        lHashMap.put("id", id);

        ArrayList<HashMap<String, String>> all = new ArrayList<>();
        for (HashMap.Entry<TQModify.AttrachInfo, String> entry:
                value.entrySet()) {
            HashMap<String, String> realValue = new HashMap<>();

            realValue.put("name", entry.getKey().name());
            realValue.put("val", entry.getValue());

            all.add(realValue);
        }

        lHashMap.put("field", all);
        TQNetClient.getShareClient().post("/api/material/attachment/update", lHashMap, pNetCallback);
    }

    static public void delMaterialAttrach(int attId, INetCallback pNetCallback){
        HashMap<String, Object> lHashMap = new HashMap<>();
        lHashMap.put("attachment_id", attId);

        TQNetClient.getShareClient().post("/api/material/attachment/del", lHashMap, pNetCallback);

    }

    static public void getMaterialReader(int mId, int page, INetCallback pNetCallback){
        String url = String.format("/api/material/%d/reader/20/%d", mId, page);

        TQNetClient.getShareClient().get(url, null, pNetCallback);
    }


    ///optimize
    static public void getMaterialLink(int mId, INetCallback pNetCallback){
        String url = String.format("/api/material/%d/chain", mId);
        TQNetClient.getShareClient().get(url, null, pNetCallback);
    }

    static public void getMeterialComment(int mId, int page, INetCallback pNetCallback){
        String url = String.format("/api/material/%d/attachmentwithcomm/20/%d", mId, page);
        TQNetClient.getShareClient().get(url, null, pNetCallback);
    }

    static public void getClsBrif(int clsId, INetCallback pNetCallback){
        String url = String.format("/api/clazz/%d/brief", clsId);
        TQNetClient.getShareClient().get(url, null, pNetCallback);
    }

    static public void modifyPwd(String cur, String nw, INetCallback pNetCallback){
        HashMap<String, Object> lHashMap = new HashMap<>();
        lHashMap.put("cur_pwd", cur);
        lHashMap.put("new_pwd", nw);

        TQNetClient.getShareClient().post("/api/teacher/password", lHashMap, pNetCallback);
    }

    static public void getClsState(String clsId, INetCallback pNetCallback){
        String url = String.format("/api/clazz/%s/stat", clsId);

        TQNetClient.getShareClient().get(url, null, pNetCallback);
    }


    /////
    static public void getAttComm(int attId, int page, INetCallback pNetCallback){
        String url = String.format("/api/material/attachment/%d/comm/20/%d", attId, page, pNetCallback);
        TQNetClient.getShareClient().get(url, null, pNetCallback);
    }
    static public void newComm(int mId ,int attId, int type, String content, INetCallback pNetCallback){
        HashMap<String, Object> lHashMap = new HashMap<>();
        lHashMap.put("material_id", mId);
        lHashMap.put("attachment_id", attId);
        lHashMap.put("content", content);
        lHashMap.put("content_type", type);

        TQNetClient.getShareClient().post("/api/comment/attachment/new", lHashMap, pNetCallback);
    }

    static public void delComm(ArrayList<Integer> ids, INetCallback pNetCallback){
        HashMap<String, Object> lHashMap = new HashMap<>();
        lHashMap.put("id", ids);

        TQNetClient.getShareClient().post("/api/comment/del", lHashMap, pNetCallback);
    }
    static {
        TQPublishType.title(0);
    }

    static public void getUHistoryMsg(int u, int page, INetCallback pNet){
        String url = String.format("/api/im/umsg/%d/20/%d", u, page);
        TQNetClient.getShareClient().get(url, null, pNet);
    }
    static public void getGHistoryMsg(int g, int page, INetCallback pNetCallback){
        String url = String.format("/api/im/gmsg/%d/20/%d", g, page);
        TQNetClient.getShareClient().get(url, null, pNetCallback);
    }
}
